package com.qfjg.controllers;

import com.qfjg.VO.ProjectsUidAndPhone;
import com.qfjg.entity.ProjectsInfo;
import com.qfjg.feigns.AuthService;
import com.qfjg.feigns.RoleInfo;
import com.qfjg.myutil.ResultData;
import com.qfjg.service.ProjectsInfoService;
import com.qfjg.webcore.auth.Pass;
import com.qfjg.webcore.auth.UserAuth;
import com.qfjg.webcore.common.Utils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Tag(name = "ProjectsInfo", description = "项目信息相关(重置版)")
@RestController
@RequestMapping("/${spring.application.name}/ProjectsInfo")
public class ProjectsInfoController {

    @Autowired
    private ProjectsInfoService projectsInfoService;
    private AuthService authService;


    public ProjectsInfoController(AuthService authService) {
        this.authService = authService;
    }


    @PostMapping("/insertData")
    @Operation(summary = "插入数据")
    @Pass
    public ResultData insert(@RequestBody ProjectsInfo projectsInfo) {

        try {
            projectsInfo.setProjectId(Utils.uuid());
            projectsInfoService.insert(projectsInfo);
            return ResultData.SUCCESS();
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultData<>(444, "出现错误: " + e.getMessage(), null);
        }
    }


    @PostMapping("/userData/{uid}")
    @Operation(summary = "获取用户数据")
    @Pass
    public ResultData getUserData(@PathVariable String uid) {

        try {
            List<ProjectsInfo> projectsByUid = projectsInfoService.getProjectsByUid(uid);
            return new ResultData(projectsByUid);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultData<>(444, "出现错误: " + e.getMessage(), null);
        }
    }


    @PostMapping("/userDataByPhone/{phone}")
    @Operation(summary = "获取用户数据")
    @Pass
    public ResultData getUserDataByPhone(@PathVariable String phone) {

        try {
            List<ProjectsInfo> projectsByUid = projectsInfoService.getProjectsByPhoneNumber(phone);
            return new ResultData(projectsByUid);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultData<>(444, "出现错误: " + e.getMessage(), null);
        }
    }


    @PostMapping("/userDataUidAndPhone")
    @Operation(summary = "获取用户数据")
    @Pass
    public ResultData getUserDataByUidAndPhone(@RequestBody ProjectsUidAndPhone uidAndPhone) {

        try {
            System.out.println(uidAndPhone);
            List<ProjectsInfo> projectsInfos = projectsInfoService.ProjectsUidAndPhone(uidAndPhone);
            System.out.println(projectsInfos);
            return new ResultData(projectsInfos);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultData<>(444, "出现错误: " + e.getMessage(), null);
        }
    }


    @PostMapping("/adminData")
    @Operation(summary = "获取管理员数据")
    public ResultData getAdminData(@RequestBody List<String> roles) {

        try {
            List<ProjectsInfo> projectsByAdmin = projectsInfoService.getProjectsByAdmin(roles);
            return new ResultData(projectsByAdmin);

        } catch (Exception e) {
            e.printStackTrace();
            return new ResultData<>(444, "出现错误: " + e.getMessage(), null);
        }
    }

    @PostMapping("/adminDataFilter")
    @Operation(summary = "管理员条件查询数据")
    public ResultData<List<ProjectsInfo>> getProjectsByAdminFilter(@RequestBody ProjectsInfo projectsInfo, HttpServletRequest request) {
        try {
            UserAuth userAuth = (UserAuth) request.getAttribute("userAuth");
            Map<String, String> tokens = (Map<String, String>) request.getAttribute("tokens");
            List<RoleInfo> childrenroles = authService.roleSelfAllChildren(tokens.get("ct"), tokens.get("_t"));
            //当前用户角色和子用户角色
            ArrayList<String> rids = new ArrayList<>(userAuth.getRoles());
            for (RoleInfo childrenrole : childrenroles) {
                rids.add(childrenrole.getRid());
            }
            System.out.println(rids);
            System.out.println(projectsInfo);
            List<ProjectsInfo> projectsByAdminFilter = projectsInfoService.getProjectsByAdminFilter(projectsInfo,rids);
            return new ResultData(projectsByAdminFilter);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultData<>(444, "出现错误: " + e.getMessage(), null);
        }
    }


    @PostMapping("/delData/{id}")
    @Operation(summary = "删除用户数据")
    @Pass
    public ResultData delData(@PathVariable String id) {

        try {
            projectsInfoService.delProjectById(id);
            return ResultData.SUCCESS();
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultData<>(444, "出现错误: " + e.getMessage(), null);
        }
    }

    @PostMapping("/updateData")
    @Operation(summary = "修改用户数据")
    @Pass
    public ResultData updateData(@RequestBody ProjectsInfo projectsInfo) {

        try {
            boolean b = projectsInfoService.updateById(projectsInfo);
            if (b) {
                return ResultData.SUCCESS();
            } else {
                return new ResultData<>(444, "修改失败！ ", null);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new ResultData<>(444, "出现错误: " + e.getMessage(), null);
        }
    }

}
